/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	PrintSample

		NewDocumentDlg.cpp

			Message handler procedure for create new document 
			dialog box.

 ------------------------------------------------------------------

 Copyright (c) 2010 Microsoft Corporation. All rights reserved.

 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
 ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
 PARTICULAR PURPOSE.

 ------------------------------------------------------------------
*/
#include "stdafx.h"
#include "PrintSample.h"

#define PRINTSAMPLE_DEFAULT_PAGE_COUNT	1L

/** 
	<module>NewDocumentDlg.cpp</module>

	<summary>
		Message handler for the create new document dialog box.</summary>

	<param name="hDlg">
		Handle to the dialog box window</param>

	<param name="message">
		Window message to process.</param>

	<param name="wParam">
		Paramter passed with window message. Usage depends on value of 
		message parameter.</param>

	<param name="lParam">
		Paramter passed with window message. Usage depends on value of 
		message parameter.</param>

	<returns>
		<p>(INT_PTR)TRUE if this procedure handled the message and not 
		further processing is necessary</p>
        <p>(INT_PTR)FALSE if the message was not handled and the message 
		should be handed by the dialog manager.</p></returns>

	<remarks>
		<p>This procedure handles the following window messages.</p>
		<p>WM_INITDIALOG: initializes the controls.</p>
		<p>WM_COMMAND - IDOK: get the number of pages from the edit control 
			and post the USER_CREATE_DOCUMENT message to the main (parent) 
			window.</p>
		<p>WM_COMMAND - IDCANCEL: end the dialog box</p> 
		</remarks>

*/
INT_PTR CALLBACK 
NewDocumentDlgProc(
		HWND hDlg, 
		UINT message, 
		WPARAM wParam, 
		LPARAM lParam
)
{
	PPRINTTHREADINFO threadInfo = NULL;

	UNREFERENCED_PARAMETER(lParam);
	
	// Get the pointer to the application's data structure
	threadInfo = (PPRINTTHREADINFO)GetWindowLongPtr(
		GetParent(hDlg), GWLP_USERDATA);

	switch (message)
	{
	case WM_INITDIALOG:		

		// Limit the number of pages in a new document to 999
		//  by only allowing three digits to be entered in the edit box
		SendMessage (GetDlgItem(hDlg,IDC_NUM_PAGES),EM_SETLIMITTEXT, 3L,0L);

		// Set default number of pages to display in the edit control
		SetDlgItemInt (
			hDlg, 
			IDC_NUM_PAGES, 
			PRINTSAMPLE_DEFAULT_PAGE_COUNT, 
			FALSE);

		// If the edit control does not have focus...
		if (GetDlgCtrlID((HWND)wParam) != IDC_NUM_PAGES)
		{
			// Set the keyboard focus to the edit control
			SetFocus(GetDlgItem(hDlg, IDC_NUM_PAGES));
			// Select all the text in the edit control
			SendMessage (
				GetDlgItem(hDlg,IDC_NUM_PAGES), 
				EM_SETSEL, 
				(WPARAM)0L, 
				(LPARAM)-1L);
			// Send false to prevent the default
			//  dialog procedure from changing
			//  these settings
			return (INT_PTR)FALSE;
		}
		else
		{
			// The edit control already has focus
			return (INT_PTR)TRUE;
		}

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			{
			BOOL valid = FALSE;

			// Read then number of pages in the 
			//  edit control
			UINT numPages = GetDlgItemInt (
								hDlg, 
								IDC_NUM_PAGES, 
								&valid, 
								FALSE);

			// If the entry is not valid
			//  use the default value
			if (!valid) numPages = PRINTSAMPLE_DEFAULT_PAGE_COUNT;

			// Post the message that will cause the 
			//   new document to be created
			PostMessage (
				GetParent(hDlg), 
				USER_CREATE_DOCUMENT, 
				(DWORD)numPages, 
				0L);

			// Close the dialog box
			EndDialog(hDlg, LOWORD(wParam));
			}
			return (INT_PTR)TRUE;
		
		case IDCANCEL:
			// Close the dialog box
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;

		default:
			// Let the default dialog procedure
			//  process this command
			return (INT_PTR)TRUE;
		}
		// Let the default dialog procedure
		//  process this message
		return (INT_PTR)FALSE;
	}
	// Let the default dialog procedure
	//  process this message
	return (INT_PTR)FALSE;
}